package com.ruoyi.web.controller.project;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.uuid.IdUtils;
import com.ruoyi.framework.web.service.TokenService;
import com.ruoyi.system.domain.Api;
import com.ruoyi.system.domain.Project;
import com.ruoyi.system.service.IApiService;
import com.ruoyi.system.service.IProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 项目管理Controller
 */
@RestController
@RequestMapping("/system/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;

    @Autowired
    private TokenService tokenService;

    @Autowired
    private IApiService apiService;
    /**
     * 查询项目管理列表
     */
    @GetMapping("/all")
    public TableDataInfo all()
    {
        startPage();
        List<Project> list = projectService.selectAllProjectList();
        return getDataTable(list);
    }

    /**
     * 查询项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:list')")
    @GetMapping("/list")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出项目管理列表
     */
    @PreAuthorize("@ss.hasPermi('system:project:export')")
    @Log(title = "项目管理", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        return util.exportExcel(list, "project");
    }

    /**
     * 获取项目管理详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:project:query')")
    @GetMapping(value = "/{projectsId}")
    public AjaxResult getInfo(@PathVariable("projectsId") String projectsId)
    {
        return AjaxResult.success(projectService.selectProjectById(projectsId));
    }

    /**
     * 新增项目管理
     */
    @PreAuthorize("@ss.hasPermi('system:project:add')")
    @Log(title = "项目管理", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Project project)
    {
        //判断新增主键是否重复
           LoginUser loginUser = tokenService.getLoginUser(ServletUtils.getRequest());
           project.setProjectsId(IdUtils.simpleUUID());
           project.setCreateBy(String.valueOf(loginUser.getUser().getUserId()));
           project.setProjectCreateTime(new Date());
           return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目管理
     */
    @PreAuthorize("@ss.hasPermi('system:project:edit')")
    @Log(title = "项目管理", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Project project)
    {
        Project selectProjectById = projectService.selectProjectById(project.getProjectsId());
        if (selectProjectById.getProjectsName().equals(project.getProjectsName())){
            project.setProjectUpdateTime(new Date());
            return toAjax(projectService.updateProject(project));
        }
        else {
            Api api=new Api();
            api.setProjectsName(selectProjectById.getProjectsName());
            List<Api> apis = apiService.selectApiList(api);
            for (Api api1 : apis) {
                api1.setProjectsName(project.getProjectsName());
                apiService.updateApi(api1);
            }
            return toAjax(projectService.updateProject(project));
        }
    }

    /**
     * 删除项目管理
     */
    @PreAuthorize("@ss.hasPermi('system:project:remove')")
    @Log(title = "项目管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{projectsIds}")
    public AjaxResult remove(@PathVariable String[] projectsIds)
    {
        for (String projectsId : projectsIds) {
            Project project = projectService.selectProjectById(projectsId);
            Api api=new Api();
            api.setProjectsName(project.getProjectsName());
            List<Api> apis = apiService.selectApiList(api);
            for (Api api1 : apis) {
                apiService.deleteApiById(api1.getApiId());
            }
        }
        return toAjax(projectService.deleteProjectByIds(projectsIds));
    }
}
